﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace QD.Utils
{
    public class ExcelHelper
    {
        public static string ExportToCSV(DataTable dt)
        {
            StringBuilder sb = new StringBuilder();
            int i, column_count = dt.Columns.Count;
            for (i = 0; i < column_count; ++i)
                sb.AppendFormat("{0}{1}", dt.Columns[i].ColumnName, i < column_count - 1 ? "," : "\n");

            foreach (DataRow dr in dt.Rows)
                for (i = 0; i < column_count; ++i)
                    sb.AppendFormat("{0}{1}", dr[i].ToString(), i < column_count - 1 ? "," : "\n");

            return sb.ToString();
        }

        private static string Filter(string input)
        {
            input = input.Replace("\"", "\"\"");
            if (input.Contains(","))
                return "\"" + input + "\"";
            else
                return input;
        }
    }
}
